﻿using HalconDotNet;
using Halcon二次开发.Controller.Controller基类;
using Halcon二次开发.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static System.Windows.Forms.MonthCalendar;

namespace Halcon二次开发.Controller.模板匹配
{
    internal class ShapController 
    {
        //寻找
        private static HObject ho_ModeContours;
        public static HTuple hv_ModelID2 = null;

        static HTuple hv_RowShape, hv_ColumnShape, hv_AngleShape, hv_ScaleShape, hv_ScoreShape;

        static HTuple hv_HomMat2DIdentity = null, hv_HomMat2DTranslate, hv_HomMat2DRotate, hv_HomMat2DScale;
        static HObject ho_AffineTransContour;

        public void FindShape(HObject image, ref HTuple hv_RowShape, ref HTuple hv_ColumnShape, ref HTuple hv_AngleShape, HSmartWindowControl WinContro = null)
        {
            HOperatorSet.GetShapeModelContours(out ho_ModeContours, hv_ModelID2, 1);
            HOperatorSet.FindScaledShapeModel(image, hv_ModelID2, -3.14, 3.14, 0.5,
       1.5, 0.3, 1, 0.8, "least_squares", 0, 0.5, out hv_RowShape, out hv_ColumnShape, out hv_AngleShape,
       out hv_ScaleShape, out hv_ScoreShape);
            HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
            HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, hv_RowShape, hv_ColumnShape, out hv_HomMat2DTranslate);
            HOperatorSet.HomMat2dRotate(hv_HomMat2DTranslate, hv_AngleShape, hv_RowShape, hv_ColumnShape,
                out hv_HomMat2DRotate);
            HOperatorSet.HomMat2dScale(hv_HomMat2DRotate, hv_ScaleShape, hv_ScaleShape, hv_RowShape, hv_ColumnShape,
                out hv_HomMat2DScale);
            HOperatorSet.SetColor(WinContro.HalconWindow,"red");
            HOperatorSet.AffineTransContourXld(ho_ModeContours, out ho_AffineTransContour, hv_HomMat2DScale);
            HOperatorSet.DispObj(ho_AffineTransContour, WinContro.HalconWindow);

        }

        HObject ho_ROI_0, ho_ImageReduced, ho_GrayImage, ho_ContoursAffinTrans;
        HTuple hv_Area, hv_RowCenter, hv_ColumnCenter;

        public ShapController()
        {
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);

            HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);

            ReadShapeID("123.shm");
        }

        public static void ReadShapeID(string path)
        {
            try
            {
                HOperatorSet.GenEmptyObj(out ho_ModeContours);

                HOperatorSet.GenEmptyObj(out ho_AffineTransContour);

                HOperatorSet.ReadShapeModel(path, out hv_ModelID2);
                HOperatorSet.GetShapeModelContours(out ho_ModeContours, hv_ModelID2, 1);

            }
            catch (Exception)
            {
                MessageBox.Show("模板逻辑类加载失败！");
            }

        }


        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="ModelNum">模板数</param>
        /// <param name="ModeContourNum">模板轮廓数</param>
        /// <param name="Path">模板路径</param>
        public void CreatShape(string NewNum, ref HTuple ModelNum, HSmartWindowControl hSmartWindowControl, string Path, HObject ho_ROI_0)
        {
            
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(StaicResouce.HImage, ho_ROI_0, out ho_ImageReduced);

            HOperatorSet.ClearWindow(hSmartWindowControl.HalconWindow);

            HOperatorSet.DispObj(ho_ImageReduced, hSmartWindowControl.HalconWindow);

            //获取第一次做模板的实物坐标
            HOperatorSet.AreaCenter(ho_ROI_0, out hv_Area, out hv_RowCenter, out hv_ColumnCenter);
            
            //HOperatorSet.ClearShapeModel(hv_ModelID);
            //创建形状模型
            HOperatorSet.CreateScaledShapeModel(ho_ImageReduced, "auto", -3.14, 3.14, "auto",
                0.5, 1.5, "auto", "auto", "use_polarity", "auto", "auto", out ModelNum);
            //ShapeClass.ho_ModeContours.Dispose();
            HObject ModeContourNum =null;
            HOperatorSet.GenEmptyObj(out ModeContourNum);
            HOperatorSet.GetShapeModelContours(out ModeContourNum, ModelNum, 1);


            HOperatorSet.DispObj(ModeContourNum, hSmartWindowControl.HalconWindow);

            HOperatorSet.DispObj(ho_GrayImage, hSmartWindowControl.HalconWindow);

            HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
            HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, hv_RowCenter, hv_ColumnCenter, out hv_HomMat2DTranslate);
            ho_ContoursAffinTrans.Dispose();
            HOperatorSet.AffineTransContourXld(ModeContourNum, out ho_ContoursAffinTrans,
                hv_HomMat2DTranslate);

            HOperatorSet.DispObj(ho_ContoursAffinTrans, hSmartWindowControl.HalconWindow);

            HOperatorSet.WriteShapeModel(ModelNum, Path);
        }

        public void Find_Rectangle2(HSmartWindowControl WinContro) { 
            
        }
    }
}
